package com.example.matetest;

import com.example.matetest.slice.HomeAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;

import java.util.ArrayList;
import java.util.List;

public class HomeAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(HomeAbilitySlice.class.getName());

        //动态申请权限
        requestPermissions();
    }

    /**
     * 申请权限
     */
    public void requestPermissions() {
        String[] permissions = {
                "ohos.permission.READ_USER_STORAGE",
                "ohos.permission.WRITE_USER_STORAGE",
                "ohos.permission.LOCATION",
                "ohos.permission.LOCATION_IN_BACKGROUND"
        };
        List<String> permissionsToProcess = new ArrayList<>();
        for (String permission : permissions) {
            //判断需要处理的权限是否可动态授权
            if (verifySelfPermission(permission) != IBundleManager.PERMISSION_GRANTED
                    && canRequestPermission(permission)) {
                permissionsToProcess.add(permission);
            }
        }
        //弹窗申请权限
        requestPermissionsFromUser(permissionsToProcess.toArray(new String[0]), 0);
    }

}
